using UnityEngine;

using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

public class TestAssetBundleEditor
{
    [MenuItem("Test/Build", false, 1)]
    public static void BuildAB()
    {
        List<AssetBundleBuild> tempAssetBundleBuildList = new List<AssetBundleBuild>();
        AssetBundleBuild tempAssetBundleBuild;

        int tempIndex = 0;
        string tempFilePath;
        string tempFileName;

        tempAssetBundleBuild = new AssetBundleBuild();
        string[] tempAllFileFullPaths = Directory.GetFiles(string.Format("{0}/TestAsset", Application.dataPath), "*.*", SearchOption.AllDirectories);
        for (int i = 0; i < tempAllFileFullPaths.Length; i++)
        {
            if(!tempAllFileFullPaths[i].EndsWith(".meta"))
            {
                tempAllFileFullPaths[i] = tempAllFileFullPaths[i].Replace("\\", "/");
                tempIndex = tempAllFileFullPaths[i].IndexOf("/Assets/");
                tempFilePath = tempAllFileFullPaths[i].Remove(0, tempIndex + 1);
                tempFileName = Path.GetFileNameWithoutExtension(tempAllFileFullPaths[i]);

                tempAssetBundleBuild = new AssetBundleBuild();
                tempAssetBundleBuild.assetBundleName = tempFileName.ToLower();
                tempAssetBundleBuild.assetNames = new string[] { tempFilePath };
                tempAssetBundleBuildList.Add(tempAssetBundleBuild);
            }
        }

        BuildPipeline.BuildAssetBundles("Assets/StreamingAssets/Version/AssetBundle", tempAssetBundleBuildList.ToArray(), BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.Android);
        AssetDatabase.Refresh();

        ShowMD5();
    }

    [MenuItem("Test/Show MD5", false, 2)]
    public static void ShowMD5()
    {
        List<string> tempStringList = new List<string>();

        string[] tempAllFilesFullPaths = Directory.GetFiles(string.Format("{0}/TestAsset", Application.dataPath), "*.*", SearchOption.AllDirectories);
        for (int i = 0; i < tempAllFilesFullPaths.Length; i++)
        {
            if (!tempAllFilesFullPaths[i].EndsWith(".meta"))
            {
                tempAllFilesFullPaths[i] = tempAllFilesFullPaths[i].Replace("\\", "/");
                tempStringList.Add(string.Format("{0}:{1}\n", Path.GetFileName(tempAllFilesFullPaths[i]), GetMD5OfFile(tempAllFilesFullPaths[i])));
            }
        }
        tempStringList.Add("---------------------------------\n");


        tempAllFilesFullPaths = Directory.GetFiles(string.Format("{0}/StreamingAssets/Version/AssetBundle", Application.dataPath), "*.*", SearchOption.AllDirectories);
        for (int i = 0; i < tempAllFilesFullPaths.Length; i++)
        {
            if (!tempAllFilesFullPaths[i].EndsWith(".meta"))
            {
                tempAllFilesFullPaths[i] = tempAllFilesFullPaths[i].Replace("\\", "/");
                tempStringList.Add(string.Format("{0}:{1}\n", Path.GetFileName(tempAllFilesFullPaths[i]), GetMD5OfFile(tempAllFilesFullPaths[i])));
            }
        }

        Debug.LogError(string.Concat(tempStringList.ToArray()));
    }

    private static string GetMD5OfFile(string targetFileFullPath)
    {
        using (MD5 tempMD5 = MD5.Create())
        {
            byte[] tempBytes = tempMD5.ComputeHash(File.ReadAllBytes(targetFileFullPath));
            StringBuilder tempStringBuilder = new StringBuilder();
            for (int i = 0; i < tempBytes.Length; i++)
            {
                tempStringBuilder.Append(tempBytes[i].ToString("x2"));
            }
            return tempStringBuilder.ToString();
        }
    }
}
